package com.xgkj.diyiketang.util;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.PopupWindow.OnDismissListener;
import android.widget.TextView;

import com.xgkj.diyiketang.R;


public class PopupHelper {

    private static final String TAG = PopupHelper.class.getSimpleName();
    private PopupWindow mPopupWindow;
    private ListView mListView;
//    private Button mBtn;
//    private TextView textView;
    
    public PopupHelper(final Context ctx, BaseAdapter adapter) {
        if (ctx == null) {
            PalLog.e(TAG, "context is null, PopupHelper construction failed, check please!");
            return;
        }
        
        View view = LayoutInflater.from(ctx).inflate(R.layout.popwindowlist, null);
        mPopupWindow = new PopupWindow(view, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        mListView = (ListView) view.findViewById(R.id.listview);
        mListView.setAdapter(adapter);
//        textView = (TextView) view.findViewById(R.id.title);
//        textView.setText(titleText);
//        mBtn = (Button) view.findViewById(R.id.cancle);
//        mBtn.setOnClickListener(new OnClickListener() {
//
//			@Override
//			public void onClick(View v) {
//				mPopupWindow.dismiss();
//			}
//		});
       mPopupWindow.setBackgroundDrawable(new ColorDrawable());// use for dismiss when touching outside
        mPopupWindow.setOutsideTouchable(true);
        mPopupWindow.setFocusable(true);
        
    }
    
    public void setOnItemClickListener(OnItemClickListener listener) {
        mListView.setOnItemClickListener(listener);
    }
    
    public void setOnDisMissListener(OnDismissListener listener){
    	mPopupWindow.setOnDismissListener(listener);
    }
    
    public void showDropDownView(View view) {
//        mPopupWindow.showAtLocation(view, Gravity.TOP, 0, 250);
        mPopupWindow.showAsDropDown(view,0,10);
//        PalLog.e(TAG,view.getLeft()+"****"+view.getRight()+"**********"+view.getTop()+"************"+view.getBaseline());
        setListViewHeightBasedOnChildren(mListView);
    }

    public void showAtLocationView(View view,int gravity,int x,int y){
        mPopupWindow.showAtLocation(view,gravity,x,y);
        setListViewHeightBasedOnChildren(mListView);
    }
    
    public void dismissDropDownView() {
        mPopupWindow.dismiss();
    }
    
    public void setOnCancleListener(OnClickListener listener){
    	//mBtn.setOnClickListener(listener);
    }
    
	public void setListViewHeightBasedOnChildren(ListView listView) {
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            return;
        }

        int totalHeight = 0;
       
        for (int i = 0; i < listAdapter.getCount(); i++) {
        	if(i>4) break;
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }
        LayoutParams params = listView.getLayoutParams();
        int count = listAdapter.getCount();
        if (count > 4){
            params.height = totalHeight + (listView.getDividerHeight() * 4);
        }else {
            params.height = totalHeight +(listView.getDividerHeight()*(count-1));
        }
        PalLog.e(TAG,listAdapter.getCount()+"*listView高**"+params.height);
        listView.setLayoutParams(params);
        listView.requestLayout();
    }
}
